var isJson = function(obj){
    var isjson = typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
    return isjson;
};
$(document).ready(function (){
    $("#register").click(function (){
        var email = $("#email").val()
        var username = $("#username").val()
        var password = $("#password").val()
        var re_password = $("#re_password").val()

        var formdata = "email="+email+"&username="+username+"&password="+password+"&re_password="+re_password
        $.ajax(
            {
                type:"POST",
                url: "/account/register",
                data:formdata,
                dataType:"json",
            }
        ).done(function (data){
            $("#registeralert").text("");
            $("#registeralert").removeClass("alert-danger");
            $("#registeralert").addClass("alert-success");
            $("#registeralert").text(data.msg);
        }).fail(function (data){
            var json = data.responseJSON.msg;
            $("#registeralert").text("");
            if (isJson(json)){
                for(x in json){
                    $("#registeralert").append(json[x]+"<br>");
                }
            }else {
                $("#registeralert").append(json+"<br>");
            }
            $("#registeralert").removeClass("alert-success");
            $("#registeralert").addClass("alert-danger");
        });
        $("#registeralert").show();
    })
})